﻿using System;
using System.Text;

public static class StringBuilderSubstring
{
    public static StringBuilder Substring(this StringBuilder builder, int index, int length)
    {
        if (index < 0)
        {
            throw new ArgumentOutOfRangeException(String.Format("Index {0} must be >= 0 ", index));
        }
        if (index > builder.Length)
        {
            throw new ArgumentOutOfRangeException(String.Format("Index {0} must be <= than builder's length ", index));
        }
        if (index > builder.Length - length)
        {
            throw new ArgumentOutOfRangeException(String.Format("Length {0} must be a smaller number", length));
        }
        if (length < 0)
        {
            throw new ArgumentOutOfRangeException(String.Format("Length {0} must be >= 0 ", length));
        }
        if (length == 0)
        {
            return new StringBuilder();
        }

        StringBuilder substringOfBuilder = new StringBuilder(length);
        for (int i = index; i <length; i++)
        {
            substringOfBuilder.Append(builder[i]);
        }
        return substringOfBuilder;
    }
}

